/** * Sumpu Inc. * Copyright (c) 2011-2012 All Rights Reserved. */
package cn.sumpu.app.guoguojie.web.mvc.shop;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.*;
import net.sf.json.*;

import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import cn.sumpu.app.guoguojie.biz.*;
import cn.sumpu.app.guoguojie.constant.Channel;
import cn.sumpu.app.guoguojie.constant.DoingShareType;
import cn.sumpu.app.guoguojie.domain.*;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.*;

/** *  * * @author james * @version $Id: ShopController.java, v 0.1 2012-8-7 下午03:33:59 james Exp $ */
@Controller
public class ShopController {

    private static Logger logger = LoggerFactory.getLogger(ShopController.class);

    @Autowired
    DoingTagShowBiz       tagBiz;
    @Autowired
    DoingBiz              doingBiz;
    @Autowired
    TopBiz                topBiz;
    @Autowired
    private TopShopBiz    topShopBiz;
    @Autowired
    private CmsContentBiz contentBiz;

    // 进入格子屋
    @RequestMapping(value = "/shop/shopping.htm")
    public String shopping(String pd, String id, String tp, Integer page, Integer mi, Integer ma, String sex, HttpServletRequest request,
                           HttpServletResponse response) {
        try {
            tagBiz.getGrid(pd, id, tp, page, 1, mi, ma, null, null, request);
            // 男孩通栏广告
            List<CmsContent> boyAdvList = contentBiz.getContentList("f64562b1-a8b8-4eeb-be94-71b303d05a73", 2);
            // 女孩通栏广告
            List<CmsContent> girlAdvList = contentBiz.getContentList("485a88a7-195c-461b-933b-67ff64b38804", 2);
            // 孕妈妈通栏广告
            List<CmsContent> motherAdvList = contentBiz.getContentList("d3a8b4c8-0807-4097-9b0a-fcc2241977cd", 2);
            //底部
            contentBiz.pushContent(request);
            request.setAttribute("pd", pd);
            request.setAttribute("boyAdvList", boyAdvList);
            request.setAttribute("girlAdvList", girlAdvList);
            request.setAttribute("motherAdvList", motherAdvList);
        } catch (AppBusinessException e) {
            logger.error("[/shop/shopping.htm]", e);
        }
        return "v3/shop/shopping";
    }

    // 加载格子屋里面更多的商品
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/shop/shoppingMore.htm")
    public String shoppingMore(String pd, String id, String tp, Integer page, Integer times, Integer mi, Integer ma, String sex, HttpServletRequest request,
                               HttpServletResponse response) {
        PageModel pageModel = tagBiz.getGrid(pd, id, tp, page, times, mi, ma, null, null, request);
        List<Doing> list = pageModel.getList();
        if(list != null && list.size() > 0){
            return "v3/shop/shoppingMore";
        }else{
            response.setContentType("text/html; charset=UTF-8");
            try {
                PrintWriter out = response.getWriter();
                out.print("");
            } catch (IOException e) {
                logger.error("[/shop/shoppingMore.htm]", e);
            }
            return null;
        }
        
    }

    // 产品详细页面   /shop/product.htm
    @RequestMapping(value = "/shop/go.htm")
    public String doingDetail(HttpServletRequest request, HttpServletResponse response, String pid) {
        try {
            doingBiz.productDetail(pid, request);
            //右边广告
            List<CmsContent> advRight = contentBiz.getContentList(Channel.ADV_RIGHT, 2);
            request.setAttribute("advRight", advRight);
            //推送的标签
            List<CmsContent> amoysTags = contentBiz.getContentList(Channel.AMOYS_Tag, 20);
            request.setAttribute("amoysTags", amoysTags);
            //话题
            List<CmsContent> topics = contentBiz.getContentList(Channel.AMOYS_TOPIC, 4);
            request.setAttribute("topics", topics);
        } catch (Exception e) {
            logger.error("微博详细页面 出错", e);
            e.printStackTrace();
        }
        return "v3/shop/dtDetail";
    }

    // 打开分享链接页面
    @RequestMapping(value = "/tosharelink.htm")
    public String tosharelink(HttpServletRequest request, HttpServletResponse response) {
        return "v3/shop/sharelink";
    }

    //分享商品  生成微博
    @RequestMapping(value = "/shareproduct.htm", method = RequestMethod.POST)
    public String saveWeiBoNew(Doing doing, String productList, String pictureList, Boolean isShare, String memberId, HttpServletRequest request,
                               HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            Member member = new Member();
            //判断是后台还是前台
            if (Common.notEmptyStr(memberId)) {
                member.setIdentifier(memberId);
            } else {
                member = OnLineUserListener.getSessionMember(request);
            }
            if (member != null && member.getEnabled()) {
                productList = Encode.unescape(productList);
                JSONArray productArray = JSONArray.fromObject(productList);
                DoingProduct product = (DoingProduct) JSONObject.toBean(JSONObject.fromObject(productArray.get(0)), DoingProduct.class);
                doing.setShareType(DoingShareType.PRODUCT.getValue());
                AlertMsg result = doingBiz.postDoingProduct(doing, product, Common.getIp(request), isShare, member);
                out.print(JSONObject.fromObject(result));
            }
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Post doing-product fail", e);
            }
        }
        return null;
    }

    // 打开推荐商品链接页面
    @RequestMapping(value = "/commendlink.htm")
    public String commendlink(HttpServletRequest request, HttpServletResponse response, String id, Double commision) {
        try {
            Member m = OnLineUserListener.getSessionMember(request);
            if (m == null || Common.emptyStr(id)) {
                return null;
            }
            DoingProduct product = doingBiz.getDpById(id);
            String yuming = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
            product.setLinkUrl(yuming + "outLink.htm?pid=" + product.getIdentifier() + "&mid=" + m.getIdentifier());
            product.setCommission(commision);
            request.setAttribute("product", product);
        } catch (Exception e) {
            logger.error("打开推荐商品链接页面 出错", e);
            e.printStackTrace();
        }
        return "v3/shop/commendLink";
    }

    /**
     * @author grieous
     * 好店
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/topshop.htm")
    public String topShop(HttpServletRequest request, HttpServletResponse response, Integer page) {
        try {
            PageModel pageModel = topShopBiz.getTopShop(page, 24, 7, null);
            request.setAttribute("pageModel", pageModel);
            List<CmsContent> advList = contentBiz.getContentList(Channel.ADV_TOPSHOP, 3);
            request.setAttribute("advList", advList);
        } catch (AppBusinessException e) {
            logger.error("[/topshop.htm] fail", e);
        }
        return "v3/shop/topShop";
    }

    /**
     * @author BruceZhang
     * @param request
     * @param response
     * @return  查询好店微博
     */
    @RequestMapping(value = "/shop.htm")
    public String shop(HttpServletRequest request, HttpServletResponse response, String snick, Integer page) {
        try {
            PageModel pageModel = tagBiz.getDoingBySnick(snick, page);
            TaobaokeShop shop = topShopBiz.getTopShopBySellerId(snick);
            List<CmsContent> advList = contentBiz.getContentList(Channel.ADV_TOPSHOP, 3);
            request.setAttribute("advList", advList);
            request.setAttribute("pageModel", pageModel);
            request.setAttribute("shop", shop);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "v3/shop/shop";
    }
    
}
